﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.WebControls;

public partial class Posts_Publish : BasePostPage
{
    public override Role.Roles[] AllowedRoles
    {
        get
        {
            return new Role.Roles[] { Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.Developer, Role.Roles.ContentPublisher };
        }
    }

    protected Role.Roles[] EditRoles
    {
        get
        {
            return new Role.Roles[] { Role.Roles.ContentEditor, Role.Roles.Developer, Role.Roles.ContentPublisher };
        }
    }

    protected Role.Roles[] PublishRoles
    {
        get
        {
            return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentPublisher };
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    public override void DataBind()
    {
        lnkDelete.Visible = lnkRetire.Visible = lnkEdit.Visible = lnkSave.Visible = lnkCancel.Visible = lnkPubilsh.Visible = lnkMark.Visible = lnkUnMark.Visible = false;
        if (CurrentPost != null)
        {
            if (CurrentPost.PublishFrom == null)
            {
                pnlPending.Visible = true;
                pnlRetire.Visible = pnlPublished.Visible = false;
                pnlPending.DataBind();
            }
            else
            {
                pnlPublished.Visible = true;
                pnlRetire.Visible = pnlPending.Visible = false;
                pnlPublished.DataBind();
            }
        }
    }

    protected void pnlPending_DataBinding(object sender, EventArgs e)
    {
        dtFrom.MinDate = DateTime.Now;
        dtFrom.SelectedDate = DateTime.Now;
        dtTo.MinDate = DateTime.Now;

        pMarked.Visible = pNotMarked.Visible = pNotRequired.Visible = false;
        pnlPublishDates.Visible = pnlPublishImmedatly.Visible = false;
        if(CurrentPost.ReadyToPublish)
        {
            lnkMark.Visible = false;
            lnkUnMark.Visible = User.HasRole(EditRoles);
            pnlPublishImmedatly.Visible = pnlPublishDates.Visible = User.HasRole(PublishRoles);
            pMarked.Visible = true;
        }
        else if(CurrentPost.RequiresPublishing)
        {
            lnkMark.Visible = User.HasRole(EditRoles);
            lnkUnMark.Visible = false;
            pnlPublishImmedatly.Visible = pnlPublishDates.Visible = User.HasRole(PublishRoles);
            pNotMarked.Visible = true;
        }
        else if (!CurrentPost.RequiresPublishing)
        {
            pNotRequired.Visible = true;
            lnkUnMark.Visible = lnkMark.Visible = false;
            pnlPublishImmedatly.Visible = pnlPublishDates.Visible = User.HasRole(PublishRoles);
        }



        lnkDelete.Visible = true;
        List<Post> allVersions = Post.GetCurrentVersions(AcmsContext.SiteId, CurrentPost.PostGuid);
        foreach (Post p in allVersions)
        {
            if (p.PublishFrom != null)
            {
                lnkDelete.Visible = false;
                break;
            }
        }
    }

    protected void pnlPublished_DataBinding(object sender, EventArgs e)
    {
        ((DateTimeField)dvPublishingDates.Fields[0]).MinValue = DateTime.Now;
        ((DateTimeField)dvPublishingDates.Fields[1]).MinValue = DateTime.Now;
        if (CurrentPost.PublishFrom < DateTime.Now)
        {
            ((DateTimeField)dvPublishingDates.Fields[0]).ReadOnly = true;
        }
        lnkRetire.Visible = lnkEdit.Visible = User != null && User.HasRole(PublishRoles);
    }
    protected void lnkMark_Click(object sender, EventArgs e)
    {
        CurrentPost.ReadyToPublish = true;
        CurrentPost.Update();
        DataBind();
    }

    protected void lnkUnMark_Click(object sender, EventArgs e)
    {
        CurrentPost.ReadyToPublish = false;
        CurrentPost.Update();
        DataBind();
    }
    protected void lnkPubilsh_Click(object sender, EventArgs e)
    {
        pnlPublishImmedatly.Visible = true;
        pnlPublishDates.Visible = true;
        lnkPubilsh.Visible = lnkMark.Visible = lnkUnMark.Visible = false;
        lnkCancel.Visible = true;
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        dvPublishingDates.ChangeMode(DetailsViewMode.ReadOnly);
        DataBind();
    }
    protected void lnkPublishNow_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish();
        AfterPublish();
    }

    protected void lnkPublishNowChildren_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish(true, false);
        AfterPublish();
    }

    protected void lnkPublishNowChildrenMarked_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish(true, true);
        AfterPublish();
    }

    protected void lnkPublishDates_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish(false, false, dtFrom.SelectedDate.Value, dtTo.SelectedDate);
        AfterPublish();
    }

    protected void lnkPublishDatesChildren_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish(true, false, dtFrom.SelectedDate.Value, dtTo.SelectedDate);
        AfterPublish();
    }

    protected void lnkPublishDatesChildrenMarked_Click(object sender, EventArgs e)
    {
        CurrentPost.Publish(true, true, dtFrom.SelectedDate.Value, dtTo.SelectedDate);
        AfterPublish();
    }

    protected void AfterPublish()
    {
        base.SetSelectedVersion(CurrentPost.PublishFrom);
        this.DataBind();
        this.ucHeader.DataBind();
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        if (User != null && User.HasRole(PublishRoles))
        {

            lnkSave.Visible = lnkCancel.Visible = true;
            lnkRetire.Visible = lnkEdit.Visible = false;
            dvPublishingDates.ChangeMode(DetailsViewMode.Edit);
        }
    }

    protected void dsCurrentPost_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(CurrentPost);
    }

    protected void lnkRetire_Click(object sender, EventArgs e)
    {
        lnkEdit.Visible = false;
        lnkCancel.Visible = true;
        pnlRetire.Visible = true;
        pnlPublished.Visible = false;
        pnlPending.Visible = false;
    }


    protected void btnRetireThis_Click(object sender, EventArgs e)
    {
        if (User != null && User.HasRole(PublishRoles))
        {
            CurrentPost.Retire();
        }
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        CurrentPost.Delete();
        Server.Transfer(ResolveUrl("Default.aspx"));
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Publish");
    }


}
